var app = getApp()
Page({
  data: {
    modalHidden:true,
    validText:"",
    phoneStateText:"",
    userName:"",
    userPhoto:"",
    isValidation:"",
    cellPhone:"",
    dataType:''
  },
  onShow: function () {
    this.isValided();
  },
  //退出登录弹窗展示
  showModal:function(){
    this.setData({
      modalHidden: !this.data.modalHidden
    })
  },
  //退出登录弹窗-确定（清除sessionId缓存重新登录）
  modalBindaconfirm: function () {
    this.setData({
      modalHidden: !this.data.modalHidden,
    });
    wx.removeStorageSync(app.globalData.sessionId);
    wx.redirectTo({
      url: '../index/index'
    })
  },
  //退出登录弹窗-取消
  modalBindcancel: function () {
    this.setData({
      modalHidden: !this.data.modalHidden,
    })
  },
  //获取商户名称及头像，根据返回数据判断手机号状态（未绑定、未验证、已验证）
  isValided:function(){
    var _this=this;
    app.request({
      url: app.globalData.apiUrl +'/mobileOfficing/api/personalCenter',
      method: "GET",
      header: {
        'Content-Type':'application/x-www-form-urlencoded',
        'sessionId': wx.getStorageSync(app.globalData.sessionId)
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.result){
          _this.setData({
            userName: res.data.data.realName,
            userPhoto: res.data.data.photo,
            isValidation: res.data.data.isValidation,
            cellPhone: res.data.data.cellPhone
          })
          if ((_this.data.isValidation == null || _this.data.isValidation == 0) && (_this.data.cellPhone == '' || _this.data.cellPhone==null)){
            _this.setData({
              phoneStateText: "绑定手机号",
              validText:"未绑定",
              dataType:'1'
            })
          } else if ((_this.data.isValidation == null || _this.data.isValidation == 0) && (_this.data.cellPhone)){
            _this.setData({
              phoneStateText: "验证手机号",
              validText: "未验证",
              dataType: '2'
            })
          } else if (_this.data.isValidation==1 && _this.data.cellPhone != '' ) {
            var cellphone = _this.data.cellPhone.substr(0, 3) + '****' + _this.data.cellPhone.substr(7)
            _this.setData({
              phoneStateText: "更换手机号",
              validText: cellphone,
              dataType: '3'
            })
          }
        }
      }
    })
  },
  goToPwd:function(){
    wx.navigateTo({
      url: '../password/password',
    })
  },
  //手机号码操作对应跳转页面
  goToPhone:function(event){
    var _this=this;
    if (event.currentTarget.dataset.type==1){
      wx.navigateTo({
        url: '../bindphone/bindphone?phone=' + _this.data.cellPhone
      });
    } else if (event.currentTarget.dataset.type == 2){
      wx.navigateTo({
        url: '../verifyphone/verifyphone?phone=' + _this.data.cellPhone
      });
    } else if (event.currentTarget.dataset.type == 3){
      wx.navigateTo({
        url: '../changephone/changephone?phone=' + _this.data.cellPhone
      });
    }
    
  }
})